package com.coocare.licence.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.coocare.licence.config.domain.AjaxResult;
import com.coocare.licence.config.plugins.logging.annotation.Logging;
import com.coocare.licence.config.resolver.ParamResolver;
import com.coocare.licence.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author adam
 * @create 2024/1/30 09:33
 */

@Tag(name = "授权登录")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Slf4j
public class AuthController {

    private final ISysUserService userService;
    private final CaptchaService captchaService;

    @Operation(summary = "登录")
    @PostMapping("login")
    @Logging("log.login")
    public AjaxResult<Map<String, Object>> login(@Parameter(description = "登录名") @RequestParam(value = "username", required = false) String username,
                                                 @Parameter(description = "密码") @RequestParam(value = "password", required = false) String password,
                                                 @Parameter(description = "验证码的二次校验参数") @RequestParam(value = "captchaVerification", required = false) String captchaVerification) {
        log.info("用户{}开始进行登录", username);
        String captchaFlag = ParamResolver.getStr("CAPTCHA_SWITCH", "0");
        if ("1".equals(captchaFlag)) {
            CaptchaVO captchaVO = new CaptchaVO();
            captchaVO.setCaptchaVerification(captchaVerification);
            ResponseModel response = captchaService.verification(captchaVO);
            if (!response.isSuccess()) {
                String msg = "system.error";
                switch (response.getRepCode()) {
                    case "0011":
                        msg = "system.parameter.not.null";
                        break;
                    case "6110":
                        msg = "system.captcha.invalid";
                        break;
                    case "6111":
                        msg = "system.captcha.error";
                        break;
                    case "6112":
                        msg = "system.captcha.get.error";
                        break;
                    default:
                        break;
                }
                return AjaxResult.failed(msg);
            }
        }
        return AjaxResult.ok(userService.login(username, password));
    }

    @Operation(summary = "退出登录")
    @PostMapping("logout")
    public AjaxResult<Object> logout() {
        StpUtil.logout();
        return AjaxResult.ok();
    }

}
